package com.right.health.app.ui.me;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.magic.basic.activity.BaseActivity;
import com.magic.basic.utils.ToastUtil;
import com.magics.http.listener.OnHttpListener;
import com.magics.http.model.Request;
import com.magics.http.model.Response;
import com.right.health.app.R;
import com.right.health.app.bean.ServiceItemBean;
import com.right.health.app.net.UserApi;
import com.right.health.app.ui.me.adapter.ServiceItemAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * @author: weming
 * @date: 2021/5/7
 * @description:我的服务详情
 */
public class MyServiceDetailActivity extends BaseActivity implements OnHttpListener, View.OnClickListener {

    private static final String KEY_ID = "ID";
    private final int HTTP_CODE_GET_LIST = 1;
    private final int HTTP_CODE_COMPLETE = 2;

    private ListView mListView;
    private ServiceItemAdapter mAdapter;
    private UserApi mUserApi;
    private ProgressDialog mProgressDialog;

    public static void launch(Activity context, String id) {
        Intent intent = new Intent(context, MyServiceDetailActivity.class);
        intent.putExtra(KEY_ID, id);
        context.startActivity(intent);
    }

    @Override
    public void onCreate(Bundle bundle, String s) {
        setContentView(R.layout.activity_my_service_pack);
    }

    @Override
    public void findView() {
        View emptyView = findViewById(R.id.empty_view);
        mListView = findViewById(R.id.listView);
        mListView.setEmptyView(emptyView);
        mAdapter = new ServiceItemAdapter(this);
        mAdapter.setOrder(true);
        mListView.setAdapter(mAdapter);
        mAdapter.setClickListener(this);
        TextView titleTv = findViewById(R.id.tv_title);
        titleTv.setText("服务详情");
    }

    @Override
    public void initialize() {
        String id = getIntent().getStringExtra(KEY_ID);
        mUserApi = new UserApi();
        mUserApi.setListener(this);
        mUserApi.addRequestCode(HTTP_CODE_GET_LIST);
        mProgressDialog = ProgressDialog.show(this, "", "加载中", false, true);
        mUserApi.getMyServiceDetail(id);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_complete) {
            int index = (int) v.getTag();
            ServiceItemBean bean = mAdapter.getItem(index);
            if (bean.getIsfinish().equals("0")) {
                mProgressDialog = ProgressDialog.show(this, "", "加载中", false, true);
                mUserApi.setTag(index);
                mUserApi.addRequestCode(HTTP_CODE_COMPLETE);
                mUserApi.finishServicePack(bean.getDetailid());
            }
        }
    }

    @Override
    public void onResponse(Request request, Response response) {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
        if (response.isResponseFail()) {
            ToastUtil.show(this, response.error);
        } else {
            if (request.requestCode == HTTP_CODE_GET_LIST) {
                try {
                    JSONObject jsonObject = new JSONObject(response.data);
                    List<ServiceItemBean> list = new Gson().fromJson(jsonObject.optString("details"), new TypeToken<List<ServiceItemBean>>() {
                    }.getType());
                    mAdapter.setDataSource(list);
                } catch (JSONException e) {
                    e.printStackTrace();
                    ToastUtil.show(this, "数据解析失败");
                }
            } else if (request.requestCode == HTTP_CODE_COMPLETE) {
                ToastUtil.show(this, "操作成功");
                try {
                    mAdapter.getItem((int)request.tag).setIsfinish("1");
                    mAdapter.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
